<?php

namespace App\Http\Middleware;

use Closure;
use Route;
use DB;

class AdminValidation
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $route = Route::current();
        $action = 'Admin\\'.class_basename($route->getActionName());
        $param = $route->parameters();
        $request->session()->put('direction',action($action,$param));

        $name = session('adminname');
        $password = session('password');
        $role = session('role');
        if((!$name) || (!$password) || (!$role)){
            return redirect('admins/login');
        }
        $user = DB::table('admins')->where('name',$name)->first();
        if($name==$user->name && $password==$user->password && $role==$user->rid){
            $request->session()->forget('direction');
            return $next($request);
        }else {
            return redirect('admins/login');
        }
    }
}
